<?php

namespace App\Http\Controllers\Home;

use App\Http\Repository\AreaRepository;
use App\Http\Repository\BannerRepository;
use App\Http\Repository\BoxRepository;
use App\Http\Repository\FragmentRepository;
use App\Http\Repository\FriendlyLinkRepository;
use App\Http\Repository\InfoRepository;
use App\Http\Repository\MessageRepository;
use App\Http\Repository\PageRepository;
use App\Http\Repository\SortRepository;
use App\Http\Servers\Home\BaseServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class CommonController extends Controller
{
    public function __construct(Request $request, BaseServer $baseServer, MessageRepository $messageRepository, FragmentRepository $fragmentRepository, BoxRepository $boxRepository, SortRepository $sortRepository, InfoRepository $infoRepository, AreaRepository $areaRepository, BannerRepository $bannerRepository, FriendlyLinkRepository $friendlyLinkRepository, PageRepository $pageRepository)
    {
//        $this->middleware('allow');
        $this->middleware('jump');
        $this->middleware('page_view');
        $this->middleware('set_time_out');

        config(['set' => $baseServer->getConfig()]);
        config(['web_logo' => \App\Image::where('id', config('set.web_logo'))->value('path')]);
        config(['web_qrcode' => \App\Image::where('id', config('set.web_qrcode'))->value('path')]);
        config(['web_ico' => \App\Image::where('id', config('set.web_ico'))->value('path')]);
        if (!\Cache::has('share')) {
            $share = [
                'pageLabels' => $pageRepository->getPageLabel(),
                'pages' => $pageRepository->getPageinfoGroupPagelabelCanShow(),
                'flinks' => $friendlyLinkRepository->getFriendlyLinkCanShow(),
                'areas' => $areaRepository->getAreaBetweenActive()[0],
                'areaslist' => $areaRepository->getAreaBetweenActive()[1],
                'allInfos' => $infoRepository->getInfoGroupChunk(),
                'sorts' => $sortRepository->getSortGroupChunk(),
                'box' => $boxRepository->getBox(),
                'banners' => $bannerRepository->getBannerGroupPath(),
                'fragment' => $fragmentRepository->getInfoGroupFragment(),
                'messageFields' => $messageRepository->getMessageFields(),
//                'message' => $messageRepository->getMessage(),
            ];
////            if (!empty(config('set.web_area'))) {
//                $share['areas'] = $areaRepository->getAreaBetweenActive()[0];
//                $share['areaslist'] = $areaRepository->getAreaBetweenActive()[1];
////            }
            \Cache::forever('share', $share);
        } else {
            $share = \Cache::get('share');
        }

        $share['messageNum'] = $messageRepository->getMessageNum();
        $share['message'] = $messageRepository->getMessage();

        $keywords = config('set.web_keywords');
        $baseServer->keywordDowork($request, $keywords);
        config(['keywords' => $keywords]);
//        $share['seo'] = implode(',', array_chunk(explode(',', $keywords), config('app.seo_num'))[0]);
        $share['seo'] = $baseServer->getSeo($keywords, config('app.seo_num'));
        //设置地区缓存
        if (!empty($request->area) && empty(config('area'))) {
            config(['area' => $request->area]);
        } else {
            config(['area' => '']);
        }

        if (!empty($share['areaslist'])) {
            foreach ($share['areaslist'] as $value) {
                foreach ($value as $v) {
                    $range[$v['domain']] = $v['name'];
                }
            }
            config(['range' => $range]);
        }

        if (!empty($share['areaslist']) && empty($request->area)) {
            $areaUrl = [];
            foreach ($share['areaslist'] as $value) {
                foreach ($value as $v) {
                    $areaUrl[] = [
                        'url' => route('index') . '/' . $v['domain'] . $_SERVER['REQUEST_URI'],
                        'name' => $v['name']
                    ];
                }
            }
            $tmpAreaUrl = array_chunk($areaUrl, config('app.area_url_num'));
            $share['areaUrl'] = $tmpAreaUrl[0];
        }

        view()->share($share);

    }
}
